<?php
$auth = Auth::getAuth('current_user');
   $userCompany = new userQueries();
   $company = $userCompany->getCompany($auth['company_id']);
?>
<div class="fl-create-organization-chart-wrapper">
	<?php  include 'layout/workspace_header.php' ?>
	<!-- <div class="fl-option-toolbar">
				<div class="fl-toolbar-left-wrapper" style="float:none;">			
					<i class="fa fa-sitemap"></i> <span>Organizational Chart</span>
				</div>
				<div class="fl-toolbar-right-wrapper">
					<ul class="fl-breadcrums">
						<li><a href="/home"><div class="home icon"></div> <i class="fa fa-caret-right"></i></a></li> 
						<li><a href="/user_view/organizational_chart">Organizational Chart <i class="fa fa-caret-right"></i></a></li> 
						<li><a href="/user_view/formbuilder">Form Builder <i class="fa fa-caret-right"></i></a></li> 
						<li><a href="/user_view/workflow">Workflow</a></li>
					</ul>
				</div>
			</div> -->
	<div class="contentBar_workspace" style="margin-top:34px">
		<div class="fl-workspace-wrapper">
			<div class="fl-workspace">
				 <div style="display:table;">
	         <div style="display:table-row">
	            <div style="display:table-cell"></div>
	            <div class="topPointerRuler-container" style="position:relative; display:table-cell;width: auto;height: 100%;border: 1px solid #ddd;border-bottom: none;">
	                  <div class="topPointerRuler-tip" style="width: 10px;background-color: #000;height: 10px;cursor: pointer;"></div>
	            </div>
	         </div>
	         <div style="display:table-row">
	            <div  class="leftPointerRuler-container" style="position:relative;display:table-cell;white-space:nowrap;vertical-align:top;width: 10px;height: 600px;border: 1px solid #ddd;">
	                  <div class="leftPointerRuler-tip" style="width: 10px;background-color: #000;height: 10px;cursor: pointer;"></div>
	            </div>
	            <div style="display:table-cell">
	              <div class="workspace orgchart_ws" style="position:relative">
	                  <!-- Node -->
	               </div>
	            </div>
	         </div>
	      </div><!-- /end of display table -->
			</div><!-- /end of fl-workspace -->
			<?php include 'layout/form_controls.php' ?>
			<!-- <div class="fl-form-properties-wrapper">
				<div class="fl-widget-head">
					<span class="widget-title"><i class="fa fa-cogs"></i> properties</span>
					<div class="fl-form-properties-toggle">
						<i class="fa fa-chevron-right"></i>
					</div>
				</div>
				<div class="fl-props-container">
					<div class="fl-form-setting-wrapper">
						<div class="fl-widget-head">
						    <span class="widget-title">Form Settings</span>
						    <span class="fl-min-max-properties" fl-show-opt="true"><i class="fa fa-plus"></i></span>
						</div>
						<div class="fl-form-setting-content fl-for-scroll">
							<label>Custom Form Size</label>
							<div class="fl-option-form-wrapper">
								<input type="text" name="" class="form-text form_set_size form_size_width" style="width: 49%;" placeholder="Width" value="1030">
								<input type="text" name="" class="form-text form_set_size form_size_height" style="width: 49%;" placeholder="Height" value="600">
								<div class="clearfix"></div>
							</div>
							<div class="fl-option-form-wrapper">
								<label>Form Size</label>
								<select class="form-select form_size">
								    <option value="8.5x11">Letter(8.5in x 11in)</option> 
								    <option value="8.5x14">Legal(8.5in x 14in)</option> 
								    <option value="7.25x10.5">Executive(7.25in x 10.5in)</option> 
								    <option value="8.27x11.69">A4(8.27in x 11.69in)</option> 
								    <option value="33.11x46.61">A0(33.11in x 46.61in)</option> 
								    <option value="23.39x33.11">A1(23.39in x 33.11in)</option> 
								    <option value="16.53x23.39">A2(16.53in x 23.39in)</option> 
								    <option value="11.69x16.53">A3(11.69in x 16.53in)</option> 
								    <option value="8.27x11.69">A4(8.27in x 11.69in)</option> 
								</select>	
							</div>
							<div class="fl-option-form-wrapper">
								<label>Show Ruler</label><Br/>
								<label>Yes: <input type="radio" class="f-show-ruler" value="yes" name="show-form-ruler-select"/></label>
								<label>No: <input type="radio" class="f-show-ruler" checked="checked" value="no" name="show-form-ruler-select"/></label>
							</div>
						</div>
					</div>
					<div class="fl-form-actions-wrapper">
						<div class="fl-widget-head">
							<span class="widget-title">Form Actions</span>
							<span class="fl-min-max-properties" fl-show-opt="true"><i class="fa fa-plus"></i></span>
						</div>
						<div class="fl-form-actions-content fl-for-scroll">
							 <button class="btn-basicBtn padding_5 cursor save_workspace tip fl-command-btn" data-original-title="Save Organizational Chart" data-workspace="Organizational Chart" data-form-type="organizational_chart"><i class="fa fa-floppy-o"></i> Save</button>
							<button class="btn-basicBtn padding_5 cursor clear_all_nodes fl-command-btn tip" data-original-title="Clear Workspace"><i class="fa fa-times"></i> Clear All</button>
							<button class="btn-basicBtn padding_5 cursor preview_form tip fl-command-btn" data-original-title="Preview Organizational Chart" data-form-type="organizational_chart"><i class="fa fa-search"></i> Preview</button>	
						</div>
					</div>
					<div class="fl-form-controls-wrapper">
						<div class="fl-widget-head">
							<span class="widget-title">Form Controls</span>
							<span class="fl-min-max-properties" fl-show-opt="true"><i class="fa fa-plus"></i></span>
						</div>
						<div class="fl-form-controls-content fl-for-scroll">
							<button class="btn-basicBtn padding_5 fl-command-btn cursor orgchart_node tip" data-original-title="Add Node" data-object-type="processNode" data-drop="workspace"><i class="fa fa-users"></i> Department</button>
	                    </div>
					</div>
				</div>
			</div> --><!-- /end of fl-form-properties -->
			<div class="clearfix"></div>
		</div> 
	</div> 
</div>
<?php
      $db = new Database();
      //$orgchart_id = functions::getMethod_replace($_GET['id'],"-","+");
      
      if(isset($_GET['id'])){
         $orgchart_id = $_GET['id'];
         //Get orgchart information and nodes query
         $orgchart = $db->query("SELECT * FROM tborgchart tbo WHERE tbo.id = {$db->escape($orgchart_id)} and tbo.is_active = 1","array");
         $orgchart_nodes_array = $db->query("SELECT *, tbo.json as tbo_json FROM tborgchart tbo LEFT JOIN tborgchartobjects tboo
                                     on tbo.id = tboo.orgChart_id 
                                     WHERE tbo.id = {$db->escape($orgchart_id)} and tbo.is_active = 1","array");
         if(count($orgchart)!=1){
            header("location:/");
         }
         if($auth['user_level_id']=="3" && $orgchart[0]['status']!=1){
             header("location:/");
         }
         // if($orgchart[0]['']){
         //    // header("location:/");
         // }
         $ws_size = json_decode($orgchart_nodes_array[0]['tbo_json'],true);
         $ws_height = $ws_size['height'];
         $ws_width = $ws_size['width'];
         //node array
         $nodes_json = array();
         //extract nodes and add to array
         foreach($orgchart_nodes_array as $orgchart_nodes){
         	$getUserOrgchart = json_decode($orgchart_nodes['json'],true);
         	$userID = $getUserOrgchart['orgchart_user_head'][0];

         	$getUserData = $db->query("SELECT * FROM tbuser WHERE id = $userID","row");
         	$getUserOrgchart['head_name'] = $getUserData['display_name'];
         	$oldPic = $getUserOrgchart['status'];

         	if($getUserData['extension']){
         		$getUserOrgchart['status'] = $userCompany->avatarPic("tbuser",$userID,"20","20","small","avatar");
         	}else{
         		$getUserOrgchart['status'] = '<img data-placement="bottom" data-original-title="Lester Sayam" src="/js/functions/create_flowchart/adminnodes/avatardefault.jpg" width="20" height="20" class="avatar userAvatar" style="width: 100%; height: 100%; padding: 0px;">';
         	}


         	// echo $oldPic." = ".$getUserOrgchart['head_name']."<br />";

         	$getUserOrgchart = json_encode($getUserOrgchart);
            array_push($nodes_json,$getUserOrgchart);
         }
         //convert array to json array
         $nodes_json =  htmlspecialchars(json_encode($nodes_json));
         
         //Get orgchart lines query
         $orgchart_lines_array = $db->query("SELECT * FROM tborgchartline 
                                     WHERE orgChart_id = {$db->escape($orgchart_id)}","array");
         $orchart_numrows = $db->query("SELECT * FROM tborgchartline 
                                     WHERE orgChart_id = {$db->escape($orgchart_id)}","numrows");
         if($orchart_numrows>0){
            //echo "1";
         }
         //lines array
         $lines_json = array();
         //extract lines and add to array
         foreach($orgchart_lines_array as $orgchart_lines){
            array_push($lines_json,$orgchart_lines['json']);
         }
         $lines_json = json_encode($lines_json);
            
      }
      ?>
      <input type="text" class="userLevel display2" value="<?php echo $auth['user_level_id']; ?>">
      <div id="orgchart_nodes" class="display"><?php echo $nodes_json; ?></div>
      <div id="orgchart_lines" class="display"><?php echo $lines_json; ?></div>
      <div id="orgchart_height" class="display"><?php echo $ws_height; ?></div>
      <div id="orgchart_width" class="display"><?php echo $ws_width; ?></div>
   </div>